//
//  MineCarsViewController.m
//  carcareIOS
//
//  Created by 王然 on 16/8/5.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "MineCarsViewController.h"
#import "MineCarCell.h"
#import "MyCarInfoInputViewController.h"
#define SECTION_HEIGHT (14)
@interface MineCarsViewController ()

@property (nonatomic , copy) NSArray <CarModel *>* carsList;

@property (nonatomic , assign) BOOL isBackBool ;//是否是需要返回上一个页面

@end

@implementation MineCarsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"我的车辆";
    
    self.isBackBool = NO;
    
    __weak __typeof(self) wself = self;
    [self navSetupRightImageName:@"vioCarsAddImage" action:^{
        MyCarInfoInputViewController * vc = [[MyCarInfoInputViewController alloc] initWithInputType:TYPE_INPUT_TABLEVIEW carModel:nil];
        vc.willBackClick = ^(BOOL isBack)
        {
            if (isBack) {
                wself.isBackBool = isBack ;
            }
        };
        [wself.navigationController pushViewController:vc animated:YES];
        
        [UmengMgr event:ICZ_CarMgrAddCar];
        [[StatisticsMgr sharedInstance] addClickID:@"我的车辆－添加车辆" description:@""];
    }];
    
    [self createTableView];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self reloadDataFinish:nil fail:nil];
}

#pragma mark - load data

-(void)loadMoreTopFinish:(void (^)(CGFloat))finish withScrollView:(UIScrollView *)scrollView{
    [self reloadDataFinish:^{
        finish(0);
    }fail:^{
        finish(0);
    }];
}

- (void)reloadDataFinish:(void(^)())finish fail:(void(^)())fail
{
    __weak __typeof(self) wself = self;
    [NetCar carListSuccess:^(NSArray <CarModel *>*cars) {
        if (cars.count > 0) {//我的车辆列表
            wself.carsList = [NSArray arrayWithArray:cars];
            [wself.tableView reloadData];
        }else//填写我的车辆
        {
            MyCarInfoInputViewController * vc = [[MyCarInfoInputViewController alloc] initWithInputType:TYPE_INPUT_TABLEVIEW carModel:nil];
            vc.willBackClick = ^(BOOL isBack)
            {
                if (isBack) {
                    wself.isBackBool = isBack ;
                }
            };
            if (wself.isBackBool) {
                [wself.navigationController popViewControllerAnimated:YES];
            }else [wself.navigationController pushViewController:vc animated:YES];
        }
        if (finish) finish();
    } fail:fail];
}
- (void)createTableView
{
    self.tableView.frame = CGRectMake(0, 0 , __MAIN_WIDTH , __MAIN_HEIGHT_64);
    self.tableView.backgroundColor = [Theme backGroundColor];
    self.tableView.separatorColor = [Theme lineNewColor];
}

#pragma mark - tableView Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.titleArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return (section == 0 ?  [self.titleArray[section] count] : self.carsList.count);
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return indexPath.section == 0 ? SECTION_HEIGHT : [MineCarCell getCellHeight];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell;
    switch (indexPath.section) {
        case 1:
        {
            NSString * cellID = @"ViolationCarCellID";
            cell = [tableView dequeueReusableCellWithIdentifier:cellID];
            if (!cell) {
                cell = [[MineCarCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            }
            [((MineCarCell *)cell) reloadDataModel:self.carsList[indexPath.row]];
        }
            break;
        case 0:
        {
            NSString * cellID = @"noCellID";
            cell = [tableView dequeueReusableCellWithIdentifier:cellID];
            if (!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
                cell.backgroundColor = [Theme backGroundColor];
            }
            if (self.carsList.count > 0 )[cell resetBorderWidth:0.5 borderColor:[Theme lineColor]];
            
        }
            break;
        default:
            break;
    }
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([Check isVisiter])
    {
        return;
    }
    [self.navigationController pushViewController:[[MyCarInfoInputViewController alloc] initWithInputType:TYPE_INPUT_TABLEVIEW carModel:self.carsList[indexPath.row]] animated:YES];
    
    [UmengMgr event:ICZ_CarMgrEditCar];
    [[StatisticsMgr sharedInstance] addClickID:@"我的车辆－编辑车辆" description:[self.tableView cellForRowAtIndexPath:indexPath].description];
}
#pragma mark - remove
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
    {
        return NO;
    }else return YES;//(!self.vioCarModels[indexPath.row].carinfoModel.unDel)
}
#pragma mark - ios8以上使用该方法删除
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    __weak __typeof(self) wself = self;
    UITableViewRowAction * rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [wself deleteDataWithIndexPath:indexPath canDelete:!wself.carsList[indexPath.row].carinfoModel.unDel];
    }];
    rowAction.backgroundColor = (self.carsList[indexPath.row].carinfoModel.unDel ? COLOR_HEXSTRING(@"#bababa") : [UIColor redColor]);
    return @[rowAction];
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //ios8以下使用该方法
    return @"删除";
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //ios8以下使用该方法
    [self deleteDataWithIndexPath:indexPath canDelete:(!self.carsList[indexPath.row].carinfoModel.unDel)];
}
#pragma mark - 右滑删除删除数据方法
- (void)deleteDataWithIndexPath:(NSIndexPath *)indexPath canDelete:(BOOL)canDelete
{
    [UmengMgr event:ICZ_CarMgrDeleteCar];
    [[StatisticsMgr sharedInstance] addClickID:@"我的车辆－删除车辆" description:[self.tableView cellForRowAtIndexPath:indexPath].description];
    
    __weak __typeof(self) wself = self;
    if (canDelete) {
        NSString * tipTitle = [self.carsList[indexPath.row].carinfoModel.brandName isNotNil] ? [NSString stringWithFormat:@"确定删除 %@ %@ 吗？",self.carsList[indexPath.row].carinfoModel.brandName,self.carsList[indexPath.row].carinfoModel.modelName] : @"确定删除吗？";
        TipsView * tipV = [TipsView textTipsWithTitle:@"" lightTitles:@[tipTitle]];
        [tipV addLeftButton:@"暂不删除" clickLeft:^{
            [AlertMgr dismiss:TYPE_VIEW_ALERT];
        } rightButton:@"确认删除" clickRight:^{
            [NetCar delCar:wself.carsList[indexPath.row].carinfoModel.id success:^{
                [wself reloadDataFinish:nil fail:nil];
            }];
            [AlertMgr dismiss:TYPE_VIEW_ALERT];

        }];
        [AlertMgr showView:tipV];
    }else
    {
        TipsView * tipV = [TipsView textTipsWithTitle:@"" lightTitles:@[@"与车挣盒子绑定的车辆无法删除"]];
        [tipV addOKButton:@"我知道了" clickOK:^{
            [AlertMgr dismiss:TYPE_VIEW_ALERT];
        }];
        [AlertMgr showView:tipV];
    }

}

- (NSArray *)titleArray
{
    return @[@[@"空白"],@[@"我的车辆列表"]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
